/*
 * ================================================================================变量定义区
 */
var currentPage_Tab2 = 1;// 全局变量，当前页
var showCount_Tab2 = 10; // 页面显示个数
var invitVoteCounts_Tab2 = -1;// 邀请投票数量
/*
 * ================================================================================页面初始化函数
 */

$(function()
{
	initContentData(currentPage_Tab2); // 初始化报表明细数据
	getDetailData(); // 初始化报表汇总数据
	drawMyChart(); // 初始化饼图数据
	catchEnterKey();// 添加监听，回车键查询
});

/*
 * ================================================================================函数定义区
 */

//导出数据到excel，先前未使用form提交方式，考虑安全性能，后期采用form表单提交方式。
function exportExcel()
{
	// 导出采用submit方式，则将查询form的值赋给导出form对应的文本框
	var surveyName = $("#survey_Name").val();
	var surveyStatus = $("#survey_Status").val();
	var id = $("#survey_Id").val();
	$("#surveyE_Name").val(surveyName);
	$("#surveyE_Status").val(surveyStatus);
	$("#surveyE_Id").val(id);
	var exportParam = getFormObj("queryForm");
	var data = JSON.stringify(exportParam);
	$("#userAnswerStatus").val(data);
	$("#queryForm").submit();
}

/**
 * 初始化统计明细数据
 * @param pageIndex
 * */
function initContentData(pageIndex)
{
	var userName = $("#survey_Name").val();
	var status = $("#survey_Status").val();
	var id = $("#survey_Id").val();
	$.ajax(
			{
				type : 'POST',
				url : BASE_PATH + '/survey/surveyStatistics/userAnswerStatus/showDetailData.do',
				dataType : "json",
				data : 
				{
					"userName" : userName,
					"status" : status,
					"surveyId" : id,
					"showCount" : showCount_Tab2,
					"currentPage" : pageIndex,
					"query_condition_field_list":"userName,status,surveyId"
				},
				success : function(data)
				{
					var pageData = data;
					var totalResult = pageData.queryParam.totalResult;
					if (invitVoteCounts_Tab2 == -1)
					{
						invitVoteCounts_Tab2 = pageData.queryParam.totalResult;
					}
					if (pageData.queryResult != null) 
					{
						// 解析data, 组装成html
						var htmlStrContent = "";
						$.each(pageData.queryResult,
										function(i, n)
										{
											if (n.status == 1)
											{
												n.status = "已投票";
											} 
											else 
											{
												n.status = "未投票";
											}
											if (n.deptName == null
													|| n.deptName == undefined) 
											{
												n.deptName = "";
											} 
											else 
											{
												n.deptName = switcHtml(n.deptName);
											}
											n.username = switcHtml(n.userName);
											n.cnname = switcHtml(n.cnName);
											n.email = switcHtml(n.email);
											var index = (Number(i) + 1);
											htmlStrContent += "<tr onclick='chooseRow(this,false)'>"
													+ "<td align='right' width='30px' >"
													+ index
													+ "</td>"
													+ "<td   align='left' width='auto' title="
													+ n.userName
													+ ">"
													+ n.userName
													+ "</td>"
													+ "<td   align='left' width='auto' title="
													+ n.cnName
													+ ">"
													+ n.cnName
													+ "</td>"
													+ "<td   align='left' width='auto' title="
													+ n.email
													+ ">"
													+ n.email
													+ "</td>"
													+ "<td   align='left' width='auto' title="
													+ n.deptName
													+ ">"
													+ n.deptName
													+ "</td>"
													+ "<td   align='center' width='65px' title="
													+ n.status
													+ ">"
													+ n.status
													+ "</td>"
													+ "<td   align='center' width='auto' title="
													+ n.createTime
													+ ">"
													+ n.createTime
													+ "</td><td style='width: 1px;' align='center'></td></tr>";
										});
						$("#tBodyListForContent").html("");
						$("#tBodyListForContent").append(htmlStrContent);
						currentPage_Tab2 = pageIndex;
						if (totalResult == 0) 
						{
							$('#tBodyListForContent')
									.html(
											'<tr><td style="text-align:center;">No items.</td></tr>');
							$("#pagination_Tab2").hide();
						} 
						else 
						{
							// 创建分页
							$("#pagination_Tab2").show();
							$("#pagination_Tab2").pagination(totalResult, 
								{
								num_edge_entries : 1, // 边缘页数
								num_display_entries : 3, // 主体页数
								current_page : currentPage_Tab2,
								callback : initContentData,
								items_per_page : showCount_Tab2
							// 每页显示1项
							     });
						};
					}
				}
			});
}

 // 查询按钮方法
function search()
{
	initContentData(1);
}

 //初始化报表统计数据
function getDetailData()
{
	var surveyId = $("#survey_Id").val();
	$.ajax(
			{
				type : 'POST',
				url : BASE_PATH + '/survey/surveyStatistics/userAnswerStatus/showDetailHead.do',
				dataType : "json",
				data :
				{
					"surveyId" : surveyId
				},
				success : function(data)
				{
					var html = "";
					if (data != null) 
					{
						data.surveyName=switcHtml(data.surveyName);
						html += '<tr><td  class="survey-info-label">问卷名称</td><td title='
								+ data.surveyName
								+ '  class="survey-info-text">'
								+ data.surveyName
								+ '</td></tr>'
								+ '<tr ><td   class="survey-info-label" >截止时间</td><td  class="survey-info-text" title='
								+ data.lastModifyTime
								+ '>'
								+ data.lastModifyTime
								+ '</td></tr>'
								+ '<tr ><td   class="survey-info-label">邀请人数</td><td  class="survey-info-text" title='
								+ data.askForCounts
								+ '>'
								+ data.askForCounts
								+ '</td></tr>'
								+ '<tr ><td   class="survey-info-label">已投票人数</td><td  class="survey-info-text" title='
								+ data.counts
								+ '>'
								+ data.counts
								+ '</td></tr>'
								+ '<tr ><td   class="survey-info-label">生成时间</td><td  class="survey-info-text" title='
								+ data.createTime
								+ '>'
								+ data.createTime
								+ '</td></tr>';
					}
					$("#tDetailMessage").html("");
					$("#tDetailMessage").html(html);
				},
				error : function()
				{
					openAlert("数据加载错误！");
				}
		  });
}

 // 初始化饼图数据
function drawMyChart()
{
	var id = $("#survey_Id").val();
	$.ajax(
	 {
		url : BASE_PATH + '/survey/surveyStatistics/userAnswerStatus/getPieData.do',
		dataType : 'json',
		data :
		{
			'surveyId' : id
		},
		type : 'post',
		success : function(data)
		{
			var pieData =  
			[{
				value : data.votedCounts,
				color : "#69D2E7"
			}, 
			{
				value : data.notVoteCounts,
				color : "#F38630"
			}];
			 new Chart(document.getElementById("myChart")
					.getContext("2d")).Pie(pieData);
			$(".totalNum").html(
					"&nbsp;&nbsp;&nbsp;(" + data.inviteVoteCounts + " 人)");
			$(".totalPhoto").attr("numReal", 1);
			$(".totalPersentage").html(1 * 100 + "%");

			$(".votedNum").html(
					"&nbsp;&nbsp;&nbsp;(" + data.votedCounts + " 人)");
			$(".votedPhoto").attr("numReal",
					data.votedCounts / data.inviteVoteCounts);
			$(".votedPersentage").html(
					(100 * (data.votedCounts / data.inviteVoteCounts))
							.toFixed(2)
							+ "%");

			$(".nonVotedNum").html(
					"&nbsp;&nbsp;&nbsp;(" + (data.notVoteCounts) + " 人)");
			$(".nonVotedPhoto").attr("numReal",
					(data.notVoteCounts / data.inviteVoteCounts));

			$(".nonVotedPersentage").html(
					(100 * ((data.notVoteCounts) / data.inviteVoteCounts))
							.toFixed(2)
							+ "%");
			$(".animateTest").each(function()
			{
				$(this).animate(
				{
					width : $(this).attr("numReal") * 400
				}, 1000);
			});
		},
		error : function()
		{
			openAlert("数据加载错误！");
		}
	});
}

 // 捕获点击回车键进行查询
function catchEnterKey()
{
	document.onkeydown = function(event)
	{
		var e = event || window.event || arguments.callee.caller.arguments[0];

		if (e && e.keyCode == 13) 
		{
			search();
		}
	};
}

// 返回按钮方法
function reBack()
{
	var type=$("#submitcurrentpage [name=type]").val();
	if(type=="finished")
	{
		$("#submitcurrentpage").submit();
	}else{
		$("#submitcurrentpage1").submit();
	}
}